/**
 * 
 */
 
var url="/tcjh/adservlet";
var projectCode;
var projectUrl;

  $(function(){
	  $("#divDetail").dialog({
		  modal: true,
          autoOpen: false,
          width: 350,
          height: 300,
//          title: "新建广告位",
//          show: true,
//          hide: true,
          buttons: {
              "Ok": function() {
			  	ADpositionSubmit();
              },
              "Cancel": function() {
              	$(this).dialog('close');
              	return false;
              }
          }
	  });
	  
	  $("#divNewMedia").dialog({
		  modal: true,
	      autoOpen: false,
	      width: 200,
	      height: 150,
	      title: "新建媒体",
	      buttons: {
	          "Ok": function() {
			  	newMediaSubmit();
	          },
	          "Cancel": function() {
	          	$(this).dialog('close');
	          	return false;
	          }
	      }
	  });
  });
  function onMouseOverRecord(id)
  {
  	$("#"+id).css("borderColor", "#666666");
	$("#"+id).css("backgroundColor", "#EEEEEE");
  }
  
  function onMouseOutRecord(id)
  {
  	$("#"+id).css("borderColor", "#EEEEEE");
	$("#"+id).css("backgroundColor", "white");
  }
  
  function onMouseOverNewRecord(id)
  {
  	$("#"+id).css("borderColor", "#EEEEEE");
	$("#"+id).css("backgroundImage", "url(images/add_over.png)");
  }
  
  function onMouseOutNewRecord(id)
  {
  	$("#"+id).css("borderColor", "white");
	$("#"+id).css("backgroundImage", "url(images/add_out.png)");
  }
  
  function addNewRecord()
  {
  	$("#selMedia").unbind("click");;
	$("#selMedia").unbind("change");
	$("#btnAddNewMedia").removeAttr("disabled");
  	$("#selMedia").removeAttr("readonly");
  	$("#txtHitCount").attr("readonly", "readonly");
	$("#txtRegisterCount").attr("readonly", "readonly");
	$("#txtAdPosition").attr("value", "");
	$("#txtFromUrl").attr("value", "");
	$("#txtHitCount").attr("value", "");
	$("#txtRegisterCount").attr("value", "");
	$("#divDetail").dialog( "option", "title", "新增" );
	refreshMediaList();
	$("#divDetail").dialog( "open" );
  }
  
  function deleteADPosition(adId)
  {
	  if(confirm("删除将不可恢复，确定删除吗？", "提示"))
	  {
		  var url="/tcjh/adservlet";
    	  var data={"op":"operation.delete", "adId":adId};
    	  sendServer(url, data, deleteResult);
	  }
  }
  
  function deleteResult(data)
  {
	  if(data == "success")
	  {
		  alert("删除成功！");
	  }
	  else if(data == "failure")
	  {
		  alert("删除失败！");
	  }
	  window.location.reload();
  }
  
  function editADPositionClick(ADId)
  {
	$("#selMedia").click(function(){this.ops=this.selectedIndex});
	$("#selMedia").change(function(){this.selectedIndex=this.ops});
	$("#btnAddNewMedia").attr("disabled", "true");
  	$("#txtFromUrl").attr("readonly", "readonly");
  	$("#txtHitCount").attr("readonly", "readonly");
	$("#txtRegisterCount").attr("readonly", "readonly");
	$("#divDetail").dialog( "option", "title", "编辑" );
	refreshMediaList();
	getADInfo(ADId);
	$("#divDetail").dialog( "open" );
  }
  
  function getADInfo(ADId)
  {
  	var data={
		"op":"operation.getADList",
		"ADId":ADId
	};
	sendServer(
		url,
		data,
		function(data)
		{
			$("#selMedia option").each(function(){
				if($(this).val() == data.mediaId)
				{
					$(this).attr("selected", "true");
				}
			})
			$("#txtAdPosition").val(data.ADName);
			$("#txtAdPosition").attr("tag", data.ADId);
			$("#txtFromUrl").val(projectUrl + "?fromId=" + data.ADId);
			$("#txtHitCount").val(data.hitCount);
			$("#txtRegisterCount").val(data.registerCount);
		},
		"json"
	);
  }
  
  function ADpositionSubmit()
  {
  	var op;
  	if($("#divDetail").dialog( "option", "title" ) == "新增")
	{
		op = "operation.add.ad";
	}
	else if($("#divDetail").dialog( "option", "title" ) == "编辑")
	{
		op = "operation.edit";
	}
  	var data={
		"op":op,
		"projectCode":projectCode,
		"ADId":$("#txtAdPosition").attr("tag"),
		"mediaId":$("#selMedia").val(),
		"ADName":$("#txtAdPosition").val(),
		"mediaName":$("#selMedia").find("option:selected").text(),
		"adUrl":$("#txtFromUrl").val(),
		"hitCount":$("#txtHitCount").val(),
		"registerCount":$("#txtRegisterCount").val()
	};
	sendServer(url, data, ADpositionResponse);
  }
  
  function ADpositionResponse(data)
  {
  	if(data=="success")
	{
//		alert(data);
		$("#divDetail").dialog("close");
		window.location.reload();
	}
	else
	{
		alert("广告位提交失败！");
	}
  }
  
  function newMediaClick()
  {
  	$("#divNewMedia").dialog("open");
  }
  
  function newMediaSubmit()
  {
  	var data={
		"op":"operation.add.media",
		"mediaName":$("#txtNewMediaName").val()
	};
	sendServer(url, data, newMediaResponse);
  }
  
  function newMediaResponse(data)
  {
  	if(String(data)=="success")
	{
//		alert(data);
		$("#divNewMedia").dialog("close");
		refreshMediaList();
	}
	else
	{
		alert(data);
		alert("新增媒体失败！");
	}
  }
  
  function refreshMediaList()
  {
  	sendServer(
		url,
		{
			"op":"operation.getMediaList"
		},
		function(json){
			$("#selMedia").empty();
			$.each(json, function(index, obj){
				$("#selMedia").get(0).options.add(new Option(obj.mediaName, obj.mediaId));
			});
		},
		"json"
	);
  }
  
  /**
   * 广告位信息类
   */
  function ADposition()
  {
  	var ADId;
	var mediaId;
	var ADName;
	var mediaName;
	var adUrl;
	var hitCount;
	var registerCount;
  }
